//
//  XDMailListViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-5-31.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDMailListViewController.h"
#import "Mail_GetList_Task.h"
#import "StringUtil.h"
#import "CommonListCell.h"
#import "MailInfoModel.h"
#import "XDMailDetailViewController.h"

@interface XDMailListViewController ()
{
    NSMutableArray *dataArray;
    NSString *getlisttaskguid;
    int pageindex;
    int pagesize;
    NSString *keyword;
    NSString *boxType;
}
@end

@implementation XDMailListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    dataArray = [[NSMutableArray alloc]init];
    pageindex = 1;
    pagesize = 20;
    keyword = @"";
    
    
    [self getData];
    boxType = [intentParams objectForKey:@"boxType"];
    // Do any additional setup after loading the view from its nib.
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tv release];
    [_searchBar release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setTv:nil];
    [self setSearchBar:nil];
    [super viewDidUnload];
}

#pragma tableview

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *WebinfolistItem = @"infolistItem";
    CommonListCell *cell = (CommonListCell *)[tableView dequeueReusableCellWithIdentifier:WebinfolistItem];
    if (cell==nil) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CommonListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    MailInfoModel *m = [dataArray objectAtIndex:indexPath.row];
    cell.lbTitle.text = m.Subject;
    
    cell.lbPerson.text = m.FROMDISPNAME;
    cell.lbDate.text = m.SENDTIME;
    if ([boxType isEqualToString:@"100000"]) {
        if ([m.haveread isEqualToString:@"1"]) {
            cell.ivStatus.hidden = YES;
            cell.lbTitle.frame = CGRectMake(7, cell.lbTitle.frame.origin.y, 307, cell.lbTitle.frame.size.height);
        }
        else{
            cell.ivStatus.hidden = NO;
        }
    }
    else
    {
        if ([m.IsAllSign isEqualToString:@"1"]) {
            cell.ivStatus.hidden = YES;
            cell.lbTitle.frame = CGRectMake(7, cell.lbTitle.frame.origin.y, 307, cell.lbTitle.frame.size.height);
        }
        else{
            cell.ivStatus.hidden = NO;
        }
    }

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 71;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MailInfoModel *m = [dataArray objectAtIndex:indexPath.row];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:m.MAILGUID forKey:@"mailguid"];
    [params setObject:[intentParams objectForKey:@"boxType"] forKey:@"boxType"];
    XDMailDetailViewController *vc = [[XDMailDetailViewController alloc]initWithParams:params];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma searchabr

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    [self doSearch:searchBar];
}

- (void)doSearch:(UISearchBar *)searchBar{
    pageindex = 1;
    [dataArray removeAllObjects];
    keyword = [searchBar text];
    [self getData];
}

#pragma control

-(void)getData
{
    [self showMBPHUD];
    self.tv.tableFooterView = [self getFooterViewLoad];
    NSMutableDictionary *params = [self getPassDictionary];
    [params setObject:[NSString stringWithFormat:@"%i",pageindex] forKey:@"CurrentPageIndex"];
    [params setObject:[NSString stringWithFormat:@"%i",pagesize] forKey:@"PageSize"];
    [params setObject:[intentParams objectForKey:@"boxType"] forKey:@"boxType"];
    [params setObject:keyword forKey:@"KeyWord"];
    
    getlisttaskguid = [[[Mail_GetList_Task alloc]initWithContext:self andParams:params]startTask];
    pageindex++;
}

-(UIView *)getFooterViewButtonNormal
{
    UIButton *lb = [[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]autorelease];
    [lb setTitle:@"加载更多..." forState:UIControlStateNormal];
    [lb setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [lb addTarget:self action:@selector(getData) forControlEvents:UIControlEventTouchUpInside];
    return lb;
}

-(UIView *)getFooterViewLoad
{
    UIActivityIndicatorView *pb = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [pb setFrame:CGRectMake(0, 0, 320, 50)];
    [pb startAnimating];
    return [pb autorelease];
}

-(void)refreshUIWithThreadGuid:(NSString *)threadGuid andData:(NSObject *)obj
{
    [self hideMBPHUD];
    if ([getlisttaskguid isEqualToString:threadGuid]) {
        NSString *bs = (NSString *)obj;
        
        NSMutableArray *dataArrayTemp = [[Mail_GetList_Task getList:bs]retain];
        
        for (int i=0; i<dataArrayTemp.count; i++) {
            [dataArray addObject:[dataArrayTemp objectAtIndex:i]];
        }
        
        self.tv.tableFooterView = [self getFooterViewButtonNormal];
        if (dataArrayTemp.count<pagesize) {
            self.tv.tableFooterView = nil;
        }
        [dataArrayTemp release];
        [self.tv reloadData];
    }
   
}


@end
